package com.geek.projects.user.web;

import com.geek.web.mvc.controller.PageController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.sql.*;

@Path("/hello")
public class HelloWorldController implements PageController {

    public static final String CREATE_USERS_TABLE_DDL_SQL = "CREATE TABLE users(" +
            "id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " +
            "name VARCHAR(16) NOT NULL, " +
            "password VARCHAR(64) NOT NULL, " +
            "email VARCHAR(64) NOT NULL, " +
            "phoneNumber VARCHAR(64) NOT NULL" +
            ")";


    @GET
    @Path("/world") // /hello/world -> HelloWorldController
    public String index(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        String databaseURL = "jdbc:derby:user-platform;create=true";
        Connection connection = DriverManager.getConnection(databaseURL);
        Statement statement = connection.createStatement();
        System.out.println(statement.execute(CREATE_USERS_TABLE_DDL_SQL)); // false
        ResultSet resultSet = statement.executeQuery(" SELECT id,name,password,email,phoneNumber FROM users ");
        ResultSetMetaData metaData = resultSet.getMetaData();
        System.out.println("当前表的名称：" + metaData.getTableName(1));
        System.out.println("当前表的列个数：" + metaData.getColumnCount());
        for (int i = 1; i <= metaData.getColumnCount(); i++) {
            System.out.println("列名称：" + metaData.getColumnLabel(i) + ", 类型：" + metaData.getColumnClassName(i));
        }
        return "index.jsp";
    }


    @GET
    @Path("/world2") // /hello/world -> HelloWorldController
    public String index2(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        return "index.jsp";
    }


    @GET
    @Path("/world3") // /hello/world -> HelloWorldController
    public String index3(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        return "index.jsp";
    }

}

